package com.haiyi.software.app.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.haiyi.software.app.ActivityAboutme;
import com.haiyi.software.app.ActivityModifyInformation;
import com.haiyi.software.app.ActivityMine;
import com.haiyi.software.app.LoginActivity;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.dao.ChatDAO;
import com.haiyi.software.tools.L;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public abstract class ActivityMineBasicMenu extends ActivityMineBasicUpload {

	private final static String TAG = "FragmentBasicPersonalMenu";

	private PopupWindow popupWindow;

	private View view;

	private ListView lv_group;

	private List<Map<String, String>> groups;

	private String[] ary = { "修改个人信息"/* , "关于我们", "退出当前帐号" */};

	private SharedPreferences sp;

	protected void showMenu(Context context, Button button) {

		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);

			view = layoutInflater.inflate(R.layout.popup_menu, null);

			lv_group = (ListView) view.findViewById(R.id.list_menu);

			// 加载数据
			groups = new ArrayList<Map<String, String>>();

			for (String item : ary) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("textView", item);
				groups.add(map);
			}

			String[] from = { "textView" };

			int[] to = { R.id.textView_menu };

			SimpleAdapter adpt = new SimpleAdapter(context, groups,
					R.layout.item_menu, from, to);

			lv_group.setAdapter(adpt);

			popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
		}

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半

		// int xPos = windowManager.getDefaultDisplay().getWidth() / 2
		//
		// - popupWindow.getWidth() / 2;
		//
		// Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
		//
		// + windowManager.getDefaultDisplay().getWidth() / 2);

		//

		// Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() /
		// 2);

		// Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(button);

		lv_group.setOnItemClickListener(itemClick);
	}

	private OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO 自动生成的方法存根
			TextView txtvView = (TextView) view;

			L.i(TAG, txtvView.getText().toString());

			if (txtvView.getText().toString().equals("修改个人信息")) {

				Intent intent = new Intent();
				intent.setClass(MainApplication.getInstance(),
						ActivityModifyInformation.class);
				startActivityForResult(intent,
						ActivityMine.SHOW_SUB_ACTIVITY_MODIFYUSERINFO);
			}

			else if (txtvView.getText().toString().equals("退出当前帐号")) {
				sp = view.getContext().getSharedPreferences("WHOESYOU",
						Context.MODE_PRIVATE);
				Editor editor = sp.edit();
				editor.putString("USER_NAME", "");
				editor.putString("PASSWORD", "");
				editor.commit();

				if (MainApplication.getInstance().getmXMPPStream()
						.GetXMPPConnection().isConnected()) {
					MainApplication.getInstance().getmXMPPStream()
							.GetXMPPConnection().disconnect();
				}

				MainApplication.getInstance().setMineUserInfo(null);

				ChatDAO.getInstence().distoryDB();

				Intent intent = new Intent();
				intent.setClass(view.getContext(), LoginActivity.class);
				startActivity(intent);
			} else if (txtvView.getText().toString().equals("关于我们")) {

				Intent intentabout = new Intent();

				intentabout.setClass(ActivityMineBasicMenu.this,
						ActivityAboutme.class);

				startActivity(intentabout);
			}
			popupWindow.dismiss();

		}
	};
}
